---@class GT.timer
---@field timeId Timer 计时器ID
GT.timer = class()

---@alias timer_callback fun(data:any,t:GT.timer):void


---@class timer_data
---@field delay Float 延迟
---@field callback timer_callback 回调函数
---@field count Int32 执行次数-1为循环
---@field immediately boolean 立即运行



---@param data timer_data
function GT.timer:ctor(data)
    self.delay = data.delay
    self.callback = data.callback
    self.count = data.count or 1
    self.immediately = data.immediately or false
    self.timeId = game_api.run_lua_timer(Fix32(self.delay), self.count, self.immediately, function(data)
        self.callback(data, self)
    end, {})
end

---转对象
---@param data timer_data
---@return GT.timer
function GT.timer.obj(data)
    return new(GT.timer, data)
end



---无限循环 立即执行
---@param time number
---@param call timer_callback 回调函数
---@return GT.timer
function GT.timer.obj2(time, call)
    ---@type timer_data
    local data = {
        callback = call,
        count = -1,
        delay = time,
        immediately = true
    }
    return new(GT.timer, data)
end

--- 计时器是否正在运行
---@return Bool
function GT.timer:is_run()
    return game_api.is_timer_valid(self.timeId)
end

--- 删除计时器
function GT.timer:delete()
    game_api.delete_timer(self.timeId)
end

--- 暂停计时器
function GT.timer:paus(timer_id)
    game_api.pause_timer(self.timeId)
end

--- 恢复计时器
function GT.timer:resume(timer_id)
    game_api.resume_timer(self.timeId)
end

--- 获取计时器设置的时间
---@return Float
function GT.timer:get_time(timer_id)
    return game_api.get_timer_time_out_time(self.timeId)
end

--- 获取计时器经过的时间
---@return Float
function GT.timer:get_elapsed_time()
    return game_api.get_timer_elapsed_time(self.timeId)
end

--- 获取计时器剩余时间
---@return Float
function GT.timer:get_remaining_time()
    return game_api.get_timer_remaining_time(self.timeId):int()
end

--- 获取计时器初始计数
---@return Int32
function GT.timer:get_init_count(timer_id)
    return game_api.get_timer_init_count(self.timeId)
end

--- 获取计时器剩余计数
---@return Int32
function GT.timer:get_remaining_count()
    return game_api.get_timer_remaining_count(self.timeId)
end

--- 获取当前到期的计时器
---@param timer_id Timer 计时器
---@return Timer
function GT.timer:get_current_expired_timer()
    return game_api.get_current_expired_timer(self.timeId)
end

--- 获取独立计时器当前计时秒数
---@param name Str 倒计时名称
---@return Fixed
function GT.timer.get_actor_timer_run_time(name)
    return game_api.get_actor_timer_run_time(name)
end

--- 计时器是否存在
---@param timer_id Timer 计时器编号
---@return Bool
function GT.timer.timer_is_exist(timer_id)
    return game_api.timer_is_exist(timer_id)
end

--- 获取最近创建的计时器
---@return Timer
function GT.timer.get_last_created_timer()
    return game_api.get_last_created_timer()
end

--- 获取游戏开始时间戳
---@return Int32
function GT.timer.get_game_init_time_stamp()
    return game_api.get_game_init_time_stamp()
end

--- 获取本地时间戳
---@return Int32
function GT.timer.get_local_time_stamp()
    return game_api.get_local_time_stamp()
end
